#!/bin/sh

help() {
  echo 
  echo "$0 <opts>"
  echo "-i - set interactive mode"
  echo "-d - set debug mode"
  echo "-? - this option"
  echo
  echo "Please ensure that PIC30_CD points to a valid install"
  echo
  exit 1
}
  
if [ ! -x $PIC30_CD/bin/pic30-gcc ]; then
  echo "Please set PIC30_CD to your compiler install path,"
  echo "eg:  export PIC30_CD=<install-path> "
  exit
fi
ECHO=`which echo`
if [ ! -x $ECHO ]; then
  echo "******* echo might be broken!"
  ECHO=echo
fi

echo "__C30_VERSION__" > version.c
VERSION=`${PIC30_CD}/bin/pic30-gcc -E version.c | grep -v "#" | sed 's/[^0-9]//g'`
debug=0
debug_skipped=0
interactive=1
export debug
export debug_skipped
export interactive
while [ "$1" != "" ]; do
  case $1 in
    "-i") interactive=0;;
    "-d") debug=1;;
    "-?") help;;
#  "-D") debug_skipped=1;;
  esac
  shift;
done
REV_A2_SUPPORTED=no
OPTS="-msmall-code -mconst-in-data -Os -I../../../include -mtext=.libperi"
if [ "$VERSION" -gt "310" ]; then
  OPTS="$OPTS -mno-file"
fi
rm -f makeplib_33E.bat
touch makeplib_33E.bat
mkdir -p ../libs
cat > makeplib_33E.bat <<HERE
@if "%1" == "" goto :build_all
  @set PROC=%1
  @goto :start
:build_all
  @set PROC=all
:start
  @set built=0
@echo off
set SAVE=
set SAVE=%PIC30_OMF%
:askagain
echo.
echo Building for Processor: %PROC%
echo.
echo Press C for COFF library
echo Press E for ELF library
echo Press Q to quit
echo.
set choice=
set /p choice=Which OMF do you use?
if '%choice%' == 'C' goto COFF
if '%choice%' == 'c' goto COFF
if '%choice%' == 'E' goto ELF
if '%choice%' == 'e' goto ELF
if '%choice%' == 'Q' goto EXIT
if '%choice%' == 'q' goto EXIT
echo.
goto askagain
:EXIT
  echo Not building a library.
  goto end
:COFF
  set PIC30_OMF=coff
  goto start1
:ELF
  set PIC30_OMF=elf
:start1
HERE
supported_targets=""
for PROCESSOR in \
		33EP256MU806 33EP256MU810 33EP256MU814 33EP512MU810 33EP512MU814
do
  supported_targets="$supported_targets $PROCESSOR"
  rm -f ../libs/libp${PROCESSOR}.a
  if [ "$REV_A2_SUPPORTED" != "no" ]; then
    rm -f ../libs/libp${PROCESSOR}A2.a
  fi
  ${ECHO} "@if \"%PROC%\" == \"all\" goto :${PROCESSOR}_S" >> makeplib_33E.bat
  ${ECHO} "@if not \"%PROC%\" == \"${PROCESSOR}\" goto :${PROCESSOR}_E" >> makeplib_33E.bat
  ${ECHO} ":${PROCESSOR}_S" >> makeplib_33E.bat
  ${ECHO} "REM build for processor ${PROCESSOR}" >> makeplib_33E.bat
  ${ECHO} "@del ..\\libs\\libp${PROCESSOR}.a" >> makeplib_33E.bat
  if [ "$REV_A2_SUPPORTED" != "no" ]; then
    ${ECHO} "@del ..\\libs\\libp${PROCESSOR}A2.a" >> makeplib_33E.bat
  fi
  olddir=`pwd`
  for dir in adc dci dma ecan hspwm i2c incap ioports outcmp qei32 \
             resetcontrol timer uart xlcd pmp comparator; do
    ${ECHO}
    ${ECHO} "-------------> $dir [$PROCESSOR] "
    cd pmc/$dir;
    ${ECHO} "@cd pmc\\${dir}" >> ../../makeplib_33E.bat
    files=`ls *.c`;
    for REV in "A2" ""; do
      if [ "$REV" = "A2" ]; then
        if [ "${REV_A2_SUPPORTED}" = "no" ]; then continue; fi
        # enumerate non A2 parts :(
        case ${PROCESSOR} in
          30F5015) continue;;
        esac
      fi
      obj_list=""
      for f in $files; do
        obj=`basename $f .c`.o
        rm -f $obj
        ${PIC30_CD}/bin/pic30-gcc -c -mcpu=${PROCESSOR}${REV} $OPTS -D__dsPIC${PROCESSOR}${REV}__ $f > .output 2>&1
        if ( grep -q "Does not build on this target" .output ); then
          if [ $debug = "1" ]; then
            ${ECHO} "***** $f Expected build failure?"
            ${ECHO} "cd pmc/$dir"
            ${ECHO} ${PIC30_CD}/bin/pic30-gcc -c -mcpu=${PROCESSOR}${REV} $OPTS -D__dsPIC${PROCESSOR}${REV}__ $f
          else
            ${ECHO} -n ".";
          fi
          continue;
        fi
        ${ECHO} -n "*"
        ${ECHO} "@echo *****  $f" >> ../../makeplib_33E.bat
        ${ECHO} "@del $obj" >> ../../makeplib_33E.bat
        ${ECHO} "@pic30-gcc -c $OPTS -mcpu=${PROCESSOR}${REV} -D__dsPIC${PROCESSOR}${REV}__ $f" >> ../../makeplib_33E.bat
        if [ ! -f $obj ]; then
           ${ECHO}
           ${ECHO} "***** $f Build failed"
           ${ECHO} "cd pmc/$dir"
           ${ECHO} ${PIC30_CD}/bin/pic30-gcc -c -mcpu=${PROCESSOR}${REV} $OPTS -D__dsPIC${PROCESSOR}${REV}__ $f
           ${ECHO} "continue? "
           if [ "$interactive" = "1" ]; then
             read go
             if [ "$go" != "y" ]; then exit; fi
           fi
        fi
        ${ECHO} "@if NOT EXIST $obj goto ERROR"  >> ../../makeplib_33E.bat
        obj_list="$obj_list $obj"
      done
      ${PIC30_CD}/bin/pic30-ar r ../../../libs/libp${PROCESSOR}${REV}.a \
                               $obj_list
      ${ECHO} "@pic30-ar r ..\\..\\..\\libs\\libp${PROCESSOR}${REV}-%PIC30_OMF%.a $obj_list" >> ../../makeplib_33E.bat
    done
  cd $olddir
  ${ECHO} "@cd ..\\.." >> makeplib_33E.bat
  ${ECHO} >> makeplib_33E.bat
  done
  ${ECHO} "@set built=1" >> makeplib_33E.bat
  ${ECHO} ":${PROCESSOR}_E" >> makeplib_33E.bat
done
cat<<EOF >> makeplib_33E.bat
@if "%built%" == "0" goto :no_target
@echo Build successful
@goto end
:ERROR
@echo Build failure
@pause
@exit
:no_target
@echo Un-recognized target: %PROC%
EOF
${ECHO} "@echo supported targets: ${supported_targets}" >> makeplib_33E.bat
${ECHO} ":end" >> makeplib_33E.bat
${ECHO} "set PIC30_OMF=" >> makeplib_33E.bat
${ECHO} "set PIC30_OMF=%SAVE%" >> makeplib_33E.bat
